package cn.moukou.tongji.ihealth.admin.service;

import cn.moukou.tongji.ihealth.admin.dao.DoctorScheduleMapper;
import cn.moukou.tongji.ihealth.admin.pojo.DoctorSchedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DoctorScheduleService {

    @Autowired
    DoctorScheduleMapper doctorScheduleMapper;

    public int insertOneSchedule(Date date, String did) {
        DoctorSchedule doctorSchedule = new DoctorSchedule();
        doctorSchedule.setDate(date);
        doctorSchedule.setDid(did);
        return doctorScheduleMapper.insert(doctorSchedule);
    }

    public ArrayList<DoctorSchedule> getSchedulesByDate(Date date) {
        Map<String, Object> map = new HashMap<>();
        map.put("date", date);
        return (ArrayList<DoctorSchedule>) doctorScheduleMapper.selectByMap(map);
    }

    public int updateSchedules(Date date, List<DoctorSchedule> schedules) {
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("date", date);
        doctorScheduleMapper.deleteByMap(deleteMap);

        for(DoctorSchedule doctorSchedule : schedules) {
            doctorScheduleMapper.insert(doctorSchedule);
        }
        return schedules.size();
    }

}
